﻿namespace FiftyOne.Foundation.Mobile.Detection
{
    using FiftyOne;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Mobile;

    public abstract class MobileCapabilities : System.Web.Mobile.MobileCapabilities
    {
        protected const string DUAL_ORIENTATION = "dualOrientation";
        protected const string POINTING_METHOD = "pointingMethod";

        internal MobileCapabilities(IDictionary capabilities)
        {
            base.Capabilities = new Hashtable(capabilities);
        }

        internal MobileCapabilities(string userAgent)
        {
            base.Capabilities = new Hashtable();
            SetJavaScript(base.Capabilities, true);
        }

        internal MobileCapabilities(HttpContext context)
        {
            base.Capabilities = new Hashtable(context.Request.Browser.Capabilities);
            SetJavaScript(base.Capabilities, GetJavascriptSupport(context));
        }

        private static bool GetJavascriptSupport(HttpContext context)
        {
            if (context.Request.AcceptTypes != null)
            {
                List<string> list = new List<string>(context.Request.AcceptTypes);
                foreach (string str in Constants.JAVASCRIPT_ACCEPTS)
                {
                    foreach (string str2 in list)
                    {
                        if (str2.StartsWith(str, StringComparison.InvariantCultureIgnoreCase))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        private string GetTagWriter()
        {
            switch ((base.Capabilities["preferredRenderingType"] as string))
            {
                case "xhtml-mp":
                case "xhtml-basic":
                    return "System.Web.UI.XhtmlTextWriter";

                case "chtml10":
                    return "System.Web.UI.ChtmlTextWriter";

                case "html4":
                    return "System.Web.UI.HtmlTextWriter";

                case "html32":
                    return "System.Web.UI.Html32TextWriter";
            }
            return "System.Web.UI.Html32TextWriter";
        }

        protected virtual void Init()
        {
            base.Capabilities["tagwriter"] = this.GetTagWriter();
        }

        internal abstract bool IsAudioFormatSupported(AudioFormats format);
        protected static void SetJavaScript(IDictionary capabilities, bool javaScript)
        {
            SetValue(capabilities, "javascript", javaScript.ToString().ToLowerInvariant());
            SetValue(capabilities, "Javascript", javaScript.ToString().ToLowerInvariant());
        }

        internal static void SetValue(IDictionary capabilities, string key, object value)
        {
            if (!(value is string) || !string.IsNullOrEmpty((string) value))
            {
                if (!capabilities.Contains(key))
                {
                    capabilities.Add(key, value);
                }
                else
                {
                    capabilities[key] = value;
                }
                EventLog.Debug(string.Format("Setting '{0}' to '{1}'", key, (value != null) ? value.ToString() : "null"));
            }
        }

        public abstract CssGroup CssBorderImageSupport { get; }

        public abstract CssGroup CssGradientSupport { get; }

        public abstract bool CssManipulation { get; }

        public abstract CssGroup CssRoundedCornerSupport { get; }

        public abstract bool CssSpriting { get; }

        public abstract bool CssSupportsWidthAsPercentage { get; }

        public abstract FiftyOne.Foundation.Mobile.Detection.DualOrientation DualOrientation { get; }

        public abstract int MaxImagePixelsHeight { get; }

        public abstract int MaxImagePixelsWidth { get; }

        public abstract bool MetaHandHeldFriendlySupported { get; }

        public abstract bool MetaMobileOptimizedSupported { get; }

        public abstract bool MetaViewportSupported { get; }

        public abstract int PhysicalScreenHeight { get; }

        public abstract int PhysicalScreenWidth { get; }

        public abstract PointingMethods PointingMethod { get; }
    }
}

